
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="/common/commonCss.jsp" />
<jsp:include page="/common/commonJs.jsp" />
<script type="text/javascript" src="${basePath }commonJs/tags/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">	
	var isFromUsr = false;
	var isToUsr = false;
	var bId = "${brandId}";
	var pkUsrId;
	var roleList =[101,102,103];
	var roleIdList = [];
	var saveResult = null;
	$(document).ready(function (){
		initPageBean();
	});
	
	function initPageBean() {
		
		var fromUsr = $("#language_a").val();
		if(fromUsr != '-1')
			{
				$("#fromUsr").val(fromUsr);
			}
		else
			{
				$("#fromUsr").val("");
			}
		$("#fromStartDateSearch").val($("#fromStartDateVal").val());
		$("#fromEndDateSearch").val($("#fromEndDateVal").val());
		$("#toStartDateSearch").val($("#toStartDateVal").val());
		$("#toEndDateSearch").val($("#toEndDateVal").val());
		var form = $("#authorizeForm");
		var pagenbean = new PageBean(form);
		pagenbean.setTableId("authorizeTable");
		pagenbean.setCallback(initTable);
		pagenbean.setFootId("authorizeFoot");
		setPagebeanObj(pagenbean);
		pagenbean.doPage();
	}
	
	function initTable(data)
	{
		if(data == null)
			{
				return;
			}
		trimJsonArray(data);
		$.each(data,function(ind, obj){
			var i = ind + 1;
			var htm = "<tr>";
			htm += "<td>" + i + "</td>";
			htm += "<td>"+ obj.fromName + "</td>";
			htm += "<td>" + obj.roleName + "</td>";
			htm += "<td>" + obj.toName + "</td>";
			htm += "<td>" + getFormatDate(new Date(obj.fromDate), "yyyy/MM/dd") + "-"+ getFormatDate(new Date(obj.toDate), "yyyy/MM/dd")+ "</td>";
			htm += "<td>" + obj.name + "</td>";
			htm += "<td><input type='button' class='btn_l' value='取消委托' pkAuthId='"+obj.pkAuthId+"' onclick=\"cancelAuthorize(this);\"></td></tr>";

			$("#authorizeTable").find("tr:eq(" + ind + ")").after(htm);
		});
	}

	function cancelAuthorize(obj)
	{
		$.messager.confirm("提醒", "确定要取消委托吗", function(r){
			if(r)
				{
				var pkAuthId = $(obj).attr("pkAuthId");
				$.ajax({
					url:"${basePath}WfmAuthorize/cancelAuthorize.shtml",
					data:{"pkAuthId":pkAuthId},
					dataType:"text",
					type:"post",
					async:false,
					success:function(result)
					{
						if(result == 1)
						{
							alertInfo($common_delete_success);
							initPageBean();
						}
						if(result == 0)
						{
							alertError($common_delete_failure);
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});
				}
		});
		
	}
	
	//展示被委托人
	function showToUsr(){
		isFromUsr = false;
		isToUsr = true;
		getToInfo(bId);
		$("#"+bId).addClass("content_hover");
		$('#x').window('open');
		
	}
	
	//展示委托人
	function showFromUsr()
	{
		isFromUsr = true;
		isToUsr = false;
		getFromInfo(bId);
		$("#"+bId).addClass("content_hover");
		$('#x').window('open');
	}
	
	//默认列出委托人员
	function getFromInfo(param){
		$.ajax({
			url:"${basePath}WfmAuthorize/selectUsesJobInfoByRole.shtml",
			data:{"fkBranId":param},
			dataType:"json",
			type:"post",
			async:false,
			success:function(result){
				trimJsonArray(result);
				var html="";
				$(result).each(function(id,item){
					if(id==0){
						html+='<tr><td><input type="radio" name="pkUsrId"  checked="checked"   fkBranId="'+item.fkBranId+'" userNames="'+item.usrName+'"  jobs="'+item.jobs+'"  value="'+item.pkUsrId+'"></td>'; 
					}else{
						html+='<tr><td><input type="radio" name="pkUsrId"  fkBranId="'+item.fkBranId+'"   userNames="'+item.usrName+'"  jobs="'+item.jobs+'" value="'+item.pkUsrId+'"></td>'; 
					}					
					html+='<td>'+item.usrName+'</td>';
					html+='<td>'+item.jobs+'</td></tr>';
				});
				$("#showUserInfo").html(html);
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});		
	}
	
	//默认列出当前被委托人员
	function getToInfo(param){
		$.ajax({
			url:"${basePath}WfmAuthorize/selectUsesJobInfo.shtml",
			dataType:"json",
			type:"post",
			async:false,
			data:{"fkBranId":param,"pkUsrId":pkUsrId},
			success:function(result){
				trimJsonArray(result);
				var html="";
				$(result).each(function(id,item){
					if(id==0){
						html+='<tr><td><input type="radio" name="pkUsrId"  checked="checked"  fkBranId="'+item.fkBranId+'"  userNames="'+item.usrName+'"  jobs="'+item.jobs+'"  value="'+item.pkUsrId+'"></td>'; 
					}else{
						html+='<tr><td><input type="radio" name="pkUsrId"  fkBranId="'+item.fkBranId+'"   userNames="'+item.usrName+'"  jobs="'+item.jobs+'" value="'+item.pkUsrId+'"></td>'; 
					}					
					html+='<td>'+item.usrName+'</td>';
					html+='<td>'+item.jobs+'</td></tr>';
				});
				$("#showUserInfo").html(html);
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});		
	}
	
	//通过门店选择用户
	function getUserInfo(params,id)
	{
		bId = id;
		$("#ZB").removeClass("content_hover");
		var fkBranId=$(params).attr("pkBranId");
		var bran = ['ZB','TJ','WX'];
		for(var i = 0 ; i<bran.length;i++){
			if(bran[i] == id)
				$("#"+id).addClass("content_hover");
			else
				$("#"+bran[i]).removeClass("content_hover");
		}
		if(isFromUsr)
			{
				getFromInfo(fkBranId);
			}
		else
			{
				getToInfo(fkBranId);
			}
	}
	
	//添加委托人或被委托人
	function addSelectUser()
	{
		$("input[name = 'pkUsrId']:checked").each(function(){
			if($(this).attr("checked"))
			{
				var usrName = $(this).attr("userNames");
				$("#fkBranId").val($(this).attr("fkBranId"));
				pkUsrId = $(this).val();
				if(isFromUsr)//选择委托人
				{
					$("#fromUsr2").val(pkUsrId);
					$.ajax({
						url:"${basePath}WfmAuthorize/findUserRoleById.shtml",
						data:{"fkUsrId":pkUsrId},
						dataType:"json",
						type:"post",
						async:false,
						success:function(result)
						{
							saveResult = result;
							$("#fromUserSelect").find("option").remove();
							$("input[name = 'radioButton']:checked").each(function(){
								if($(this).attr("checked")&& $(this).val()=="2")
									{
										var size = 0;
										$.each(result,function(ind,obj)//委托人角色
												{
												size++;
												});
										if(size > 1)//委托人角色多于一种
											{
											$("#fromUserSelect").append("<option value='-1' selected='selected'>全部</option>");
												$.each(result,function(ind,obj)//委托人角色
													{
														roleIdList.push(obj.fkRoleId);
														$("#fromUserSelect").append("<option value='"+obj.fkRoleId+"'>"+obj.roleName+"</option>");
													});
											}
										else
											{
												$.each(result,function(ind,obj)//委托人角色
													{
														roleIdList.push(obj.fkRoleId);
														$("#fromUserSelect").append("<option value='"+obj.fkRoleId+"'>"+obj.roleName+"</option>");
													});
											}
									}
							});
							refreshSelect();
							$("#fromUserVal").val(usrName);
							$("#toUserBtn").removeAttr("style");
							$("#toUserBtn").attr("disabled",false);
							$('#x').window('close');
						},
						error : function(XMLHttpRequest) {
							ajaxError(XMLHttpRequest);
						}
					});
				}
			else
				{
					$("#toUsr").val(pkUsrId);
					$("#toUserVal").val(usrName);
					$('#x').window('close');
				}
			}
		});
	}
	
	//打开添加对话盒
	function openDialog()
	{
		$("input[name='radioButton'][value='2']").attr("checked",'checked'); 
		radioFun();
		$("#fromUserVal").val("");
		$("#toUserVal").val("");
		$("#fromDateVal").val("");
		$("#toDateVal").val("");
		$("#fromUserSelect").find("option").remove();
		$("#toUserBtn").attr("disabled",true);
		$("#toUserBtn").attr("style","background:#737373");
		$("#language_v option[id='activeOpt']").attr("selected",true);
		$("#selectAllActive").attr("checked",false);
		$("#radioRole").attr("checked",true);
		refreshSelect();
		$('#e').window('open');
	}
	
	//新增
	function addNewWfm()
	{
		var fromVal = $.trim($("#fromUserVal").val());
		var toVal = $.trim($("#toUserVal").val());
		if(fromVal == ""  || toVal == "")
			{
				alertError("请先选择委托或被委托人员！");
				return;
			}
		var fromDate = $("#fromDateVal").val();
		var toDate = $("#toDateVal").val();
		if(fromDate == "" || toDate == "")
			{
				alertError("请先添加委托有效期！");
				return;
			}
		var activeVal = $("#language_v").val();
		if(activeVal == "")
			{
			if($("#selectAllActive").attr("checked"))
				{
					activeVal = $("#selectAllActive").val();
				}
			
			if(activeVal == "")
				{
					alertError("请先选择某个流程或者全部流程！");
					return;
				}
			}
		$("#fromDate").val(fromDate);
		$("#toDateValue").val(toDate);
		$("#roleIdListVal").val(roleIdList);
		$("#procDefId").val(activeVal);
		$.ajax({
			url:"${basePath}WfmAuthorize/insertWfmAuthorize.shtml",
			data:$("#insertauthorizeForm").serialize(),
			dataType:"json",
			type:"post",
			async:false,
			success:function(result)
			{
				if(result == 1)
					{
						$('#e').window('close');
						initPageBean();
						alertInfo("添加成功");
					}
				if(result == 0)
					{
						alertError("添加失败请检查网络");
					}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
	
	function selectAllActive(obj)
	{
		
		if($(obj).attr("checked"))
			{
				$("#language_v option[id='activeOpt']").attr("selected",true);
				refreshSelect();
			}
	}
	
	function activeOptSelect()
	{
		var activeVal = $("#language_v").val();
		if(activeVal != "")
			{
				$("#selectAllActive").attr("checked",false);
				$("#qblc").hide();
			}else
				$("#qblc").show();
	}
	
	function radioFun()
	{
		$("input[name = 'radioButton']:checked").each(function(){
			if($(this).attr("checked")&& $(this).val()=="1")
				{
					$("li[id=wtjs]").hide();
					$("div[id=qblc]").hide();
					$("#fromUserSelect").find("option").remove();
					roleIdList = [];
					$("#language_v").find("option").remove();
					$("#language_v").append("<option value='${activitiEntity.id}' selected='selected'>${activitiEntity.name}</option>");
				}
			else 
				{
					$("li[id=wtjs]").show();
					$("div[id=qblc]").show();
					$("#language_v").find("option").remove();
					$("#language_v").append("<option id='activeOpt' value='' selected='selected'>请选择</option>");
				 	<c:forEach items="${activList }" var="activ" >
				 		$("#language_v").append("<option value='${activ.id }'>${activ.name}</option>"); 
	               	</c:forEach>
                 if(saveResult != null && saveResult != '')
                	 {
                	 var size = 0;
 					$.each(saveResult,function(ind,obj)//委托人角色
 							{
 								size++;
 							});
 					if(size > 1)//委托人角色多于一种
 						{
 						$("#fromUserSelect").append("<option value='-1' selected='selected'>全部</option>");
 							$.each(saveResult,function(ind,obj)//委托人角色
 								{
 									roleIdList.push(obj.fkRoleId);
 									$("#fromUserSelect").append("<option value='"+obj.fkRoleId+"'>"+obj.roleName+"</option>");
 								});
 						}
 					else
 						{
 							$.each(saveResult,function(ind,obj)//委托人角色
 								{
 									roleIdList.push(obj.fkRoleId);
 									$("#fromUserSelect").append("<option value='"+obj.fkRoleId+"'>"+obj.roleName+"</option>");
 								});
 						}
                	 }
                 	if($('#fromUserVal').val() == ''){
                 		$("#fromUserSelect").find("option").remove();
                 	}
				}
		});
		$("#selectAllActive").attr("checked",false);
		refreshSelect();
	}
</script>
<title>嘉童游乐中心-审批转授权</title>
</head>
<body class="bg">
<div class="content m_auto">
	<div class="content_l mt10">
    	<ul>
        
            	<shiro:hasPermission name="authority/role/home.shtml">
         <li><a href="${basePath}authority/role/home.shtml"><i class="nav_icon_u"></i><br>角色管理</a></li>
           </shiro:hasPermission>
           <shiro:hasPermission name="authority/user/home.shtml">
            <li ><a href="${basePath}authority/user/home.shtml"><i class="nav_icon_t"></i><br>用户管理</a></li>
           </shiro:hasPermission>
           <shiro:hasPermission name="SalesRelation/mem.shtml">
            <li><a href="${basePath}SalesRelation/mem.shtml"><i class="nav_icon_s"></i><br>销售关联会员</a></li>
            </shiro:hasPermission>
            <shiro:hasPermission name="ParRoomDetail/ParRoom.shtml">
            <li><a href="${basePath}ParRoomDetail/ParRoom.shtml"><i class="nav_icon_r"></i><br>房间管理</a></li>
           </shiro:hasPermission>
           <shiro:hasPermission name="SysBarCode/system.shtml">
            <li><a href="${basePath }SysBarCode/system.shtml"><i class="nav_icon_q"></i><br>条码维护</a></li>
            </shiro:hasPermission>
            <shiro:hasPermission name="WfmAuthorize/toShenP.shtml">
            <li  class="content_hover"><a href="${basePath}WfmAuthorize/toShenP.shtml"><i class="nav_icon_s"></i><br>审批转授权</a></li>
            </shiro:hasPermission>
            <shiro:hasPermission name="holiday/sysHoliday.shtml">
            <li><a href="${basePath}holiday/sysHoliday.shtml"><i class="nav_icon_q"></i><br>节假日管理</a></li>
            </shiro:hasPermission>
        
        </ul>
    </div>
    <div class="content_r mt10">
    	<!---- 内容 --->
        <div class="p20">
        <div class="file_single_dl">
        	<dl>
            	<dd><label>委托人：</label>
                	<div class="uboxstyle">
                        <select name="language_a" id="language_a">
                       		<option value="-1" selected="selected">全部</option>
                            <c:forEach items="${fromUsers}" var = "users">;
                            <option value="${users.fromUsr}">${users.fromName}</option>
                            </c:forEach>
                        </select>
                    </div>
                </dd>
                <dd><label>委托日期开始：</label>
                	<input type="text" id="fromStartDateVal" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,maxDate:'#F{$dp.$D(\'fromEndDateVal\')}'})" class="Wdate iput_t w82">
<span>至</span><input type="text" id="fromEndDateVal" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,minDate:'#F{$dp.$D(\'fromStartDateVal\')}'})" class="Wdate iput_t w82">
                </dd>
                <dd><label>委托日期结束：</label>
                	<input type="text" id="toStartDateVal" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,maxDate:'#F{$dp.$D(\'toEndDateVal\')}'})" class="Wdate iput_t w82">
<span>至</span><input type="text" id="toEndDateVal" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,minDate:'#F{$dp.$D(\'toStartDateVal\')}'})" class="Wdate iput_t w82">
                </dd>
                <dd class="fr">
                	<input type="button" class="btn fr ml10" value="新增" onclick="openDialog();">
                    <input type="button" class="btn fr ml10" value="查询" onclick="initPageBean();">
            	</dl>
        </div>
        
        <table class="table" id="authorizeTable">
            <tr>
                <th>序号</th>
                <th>委托人</th>
                <th>委托角色</th>
                <th>被委托人</th>
                <th>委托日期</th>
                <th>委托流程</th>
                <th>操作</th>
            </tr>
        </table>
        <!----翻页----->
        <div class="page fr m10" id="authorizeFoot">
        </div>
        <!----翻页----->
        </div>
        <!---- 内容 --->
    </div>
</div>

<form action="${basePath}WfmAuthorize/findWfmAuthorize.shtml" id="authorizeForm" method="post">
	<input type="hidden" name="pageSize" value="10"> 
	<input type="hidden" name="pageNo" value="1"> 
	<input type="hidden" name="fromUsr" id="fromUsr">
	<input type="hidden" name="fromStartDateSearch" id="fromStartDateSearch">
	<input type="hidden" name="fromEndDateSearch" id="fromEndDateSearch">
	<input type="hidden" name="toStartDateSearch" id="toStartDateSearch">
	<input type="hidden" name="toEndDateSearch" id="toEndDateSearch">
</form>
<!-- <form id="insertauthorizeForm">
	<input type="hidden" name="fkBranId" id="fkBranId">
	<input type="hidden" name="fromUsr" id="fromUsr2">
	<input type="hidden" name="toUsr" id="toUsr">
	<input type="hidden" name="fromDate" id="fromDate">
	<input type="hidden" name="toDateValue" id="toDateValue">
	<input type="hidden" name="roleIdList" id="roleIdListVal">
	<input type="hidden" name="procDefId" id="procDefId">
</form> 
<form action="${basePath}WfmAuthorize/insertWfmAuthorize.shtml" id="addAuthorizeForm" method="post">
</form>-->
<!---弹出层--->
<div id="e" class="easyui-window" title="新增" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:600px; height:465px; position:relative;">
	<form id="insertauthorizeForm">
	<input type="hidden" name="fkBranId" id="fkBranId">
	<input type="hidden" name="fromUsr" id="fromUsr2">
	<input type="hidden" name="toUsr" id="toUsr">
	<ul class="w360_c yonh_x_ul">
    	<li class="w540"><label><span class="color_red ml5">*</span>委&nbsp;&nbsp;&nbsp;&nbsp;托人：</label><input type="text"  class="iput_t w200" id="fromUserVal"  readonly="readonly"><input type="button" class="btn_l ml5" value="选择" onclick="showFromUsr();"></li>
    	 <li class="w540"><label></label><input type="radio" id="radioRole" name="radioButton" class="ml20" value="2" onclick="radioFun();" checked="checked">委托角色<input type="radio" id="radioTask" name="radioButton" class="ml20" value="1" onclick="radioFun();">委托任务</li>
        <li id='wtjs'><label>委托角色：</label>
        	<!----select--->
            <div class="uboxstyle">
                <select name="fkRoleId" id="fromUserSelect">
                </select>
            </div>
            <!----select--->
        </li>
        <li class="w540"><label><span class="color_red ml5">*</span>被委托人：</label><input type="text" class="iput_t w200" id="toUserVal"  readonly="readonly"><input type="button" class="btn_l ml5" id="toUserBtn" value="选择 " style="background-color:#737373 " disabled="disabled" onclick="showToUsr();" ></li>
        <li><label><span class="color_red ml5">*</span>委托日期：</label>
			<input type="text" id="fromDateVal" name="fromDate" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,maxDate:'#F{$dp.$D(\'toDateVal\')}'})" class="Wdate iput_t w82">
<span>至</span><input type="text" id="toDateVal" name="toDate" onclick="WdatePicker({dateFmt:'yyyy/MM/dd',isShowWeek:true,isShowClear:true,readOnly:true,minDate:'#F{$dp.$D(\'fromDateVal\')}'})" class="Wdate iput_t w82"></li>
		<li class="w540"><label><span class="color_red ml5">*</span>委托流程：</label>
        	<!----select--->
            <div class="uboxstyle" >
                <select name="procDefId" id="language_v" onchange="activeOptSelect();">
                	<option id="activeOpt" value="" selected="selected">请选择</option>
                    <c:forEach items="${activList }" var="activ">
                    <option value="${activ.id }">
                    ${activ.name}
                    </option>
                    </c:forEach>
                </select>
            </div>
            <!----select--->
        	<div id='qblc'><input type="checkbox" id="selectAllActive" name="isAllProc" onchange="selectAllActive(this);" value="-1" class="mr5">全部流程</div></li>
        	
    </ul></form>
    <div class="btm_btn btm_bg">
    	<dl>
        	<dd><input type="button" class="btn" onclick="addNewWfm();" value="确定"></dd>
            <dd><input type="button" class="btn" onclick="$('#e').window('close')" value="返回"></dd>
        </dl>
    </div>
</div>

<!-- 人员选择框 -->
<div id="x" class="easyui-window" title="选择处理人" data-options="modal:true,closed:true,iconCls:'icon-save'" 
 style="width:620px; height:420px; position:relative;">
    <div class="m10">
    	<dl class="order_add_peic fl">
            <dd style="text-indent:12px;">嘉童</dd>
            <c:forEach items="${branList}" var="item">
            	<dd id="${item.pkBranId}" ><a href="#" pkBranId="${item.pkBranId}" onclick="getUserInfo(this,'${item.pkBranId}');" >${item.branName}</a></dd>
            </c:forEach>
        </dl>
        
        <div id="scrollTable" class="file_apply_m fl" style="width:440px !important; margin-top:0 !important">
            <table class="table">
                <col width="80px"></col>
                <col width="160px"></col>
                <col></col>
                <tbody>
                    <tr>
                        <th>选择</th>
                        <th>姓名</th>
                        <th>职位</th>
                    </tr>
                </tbody>
            </table>
			<div style="height:270px !important">
            <table class="table" id="selectProduct" >
				<col width="80px"></col>
                <col width="160px"></col>
                <col></col>
                <tbody id="showUserInfo">
                
                </tbody>
            </table>
            </div>
        </div>
</div>
  <div class="btm_btn btm_bg">
    	<dl>
        	<dd><input type="button" class="btn" onClick="addSelectUser();" value="确定"></dd>
            <dd><input type="button" class="btn" onClick="$('#x').window('close');" value="取消"></dd>
        </dl>
    </div>
</div>

</body>
</html>